package com.begye.smartloan.apptest.huankuan.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.ext.toThousandUnitFormat
import org.json.JSONObject

class HuanKuanBeforeAdapter(
    var dataList: List<JSONObject>,
    var onClickAction: (JSONObject) -> Unit
) :
    RecyclerView.Adapter<HuanKuanBeforeAdapter.ViewHolder>() {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.list_item_huan_kuan_before, parent, false)
        return ViewHolder(view)
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        private val listItemHuanKuanItemsContent: LinearLayout =
            view.findViewById(R.id.listItemHuanKuanItemsContent)

        private val listItemHuanKuanDaysLeft: TextView =
            view.findViewById(R.id.listItemHuanKuanDaysLeft)

        private val listItemHuanKuanSubmit: TextView =
            view.findViewById(R.id.listItemHuanKuanSubmit)

        private val huanKuanSubItemsContent: LinearLayout =
            view.findViewById(R.id.huanKuanSubItemsContent)

        private val huanKuanItemValue1: TextView =
            view.findViewById(R.id.huanKuanItemValue1)

        private val huanKuanItemValue2: TextView =
            view.findViewById(R.id.huanKuanItemValue2)

        private val huanKuanItemValue3: TextView =
            view.findViewById(R.id.huanKuanItemValue3)

        private val huanKuanItemValue4: TextView =
            view.findViewById(R.id.huanKuanItemValue4)

        private val huanKuanItemValue5: TextView =
            view.findViewById(R.id.huanKuanItemValue5)

        private val huanKuanItemValue6: TextView =
            view.findViewById(R.id.huanKuanItemValue6)

        private val listItemHuanKuanTopPic: ImageView =
            view.findViewById(R.id.listItemHuanKuanTopPic)

        @SuppressLint("SetTextI18n")
        fun bindData(position: Int, item: JSONObject) {
            val daysLeft = item.optInt("daysLeft", 0)

            if (daysLeft > 0) {
                listItemHuanKuanDaysLeft.setTextColor(Color.parseColor("#BEFF6A"))
                listItemHuanKuanTopPic.setImageResource(R.drawable.dres_148)

                listItemHuanKuanDaysLeft.text = listItemHuanKuanDaysLeft.context.getString(
                    R.string.huan_kuan_text_1,
                    daysLeft.toString()
                )

            } else if (daysLeft == 0) {
                listItemHuanKuanDaysLeft.setTextColor(Color.parseColor("#BEFF6A"))
                listItemHuanKuanTopPic.setImageResource(R.drawable.dres_148)

                listItemHuanKuanDaysLeft.text =
                    listItemHuanKuanDaysLeft.context.getString(R.string.huan_kuan_text_37)

            } else {
                listItemHuanKuanDaysLeft.setTextColor(Color.parseColor("#FF7373"))
                listItemHuanKuanTopPic.setImageResource(R.drawable.dres_106)

                val reverDaysLeft = -1 * daysLeft
                listItemHuanKuanDaysLeft.text = listItemHuanKuanDaysLeft.context.getString(
                    R.string.huan_kuan_text_36,
                    reverDaysLeft.toString()
                )
            }

            huanKuanItemValue1.text = item.optString("totalP", "").toThousandUnitFormat()
            huanKuanItemValue2.text = item.optString("pDate", "")
            huanKuanItemValue3.text = item.optString("jinE", "").toThousandUnitFormat()
            huanKuanItemValue4.text = item.optString("fee", "").toThousandUnitFormat()
            huanKuanItemValue5.text = item.optString("overDay", "")
            huanKuanItemValue6.text = item.optString("overJinE", "").toThousandUnitFormat()

            listItemHuanKuanSubmit.setOnClickListener {
                onClickAction.invoke(item)
            }
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindData(position, dataList[position])
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}